package data.join.com.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import org.greenrobot.eventbus.EventBus;

import data.join.com.application.EventMessage;
import data.join.com.carapi.CarIntents;
import data.join.com.media.VideoScannerThread;
import data.join.com.utils.MyLog;

/**
 * Created by Administrator on 2017/9/26.
 */

public class FileReceiver extends BroadcastReceiver {

    Handler mHandler = new Handler();

    Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;

        String action = null;
        if(intent != null){
            action = intent.getAction();
        }

        MyLog.D("FileReceiver onReceive:"+action);

        if(CarIntents.ACTION_RECORD_FILE.equals(action)) {
            mHandler.removeCallbacks(wakeUpRunnable);
            mHandler.postDelayed(wakeUpRunnable, 10 * 1000);
        }else if(CarIntents.ACTION_DELETE_FILE.equals(action)){
            EventBus.getDefault().post(new EventMessage(EventMessage.DEVICES_REGISTER_SUCCESS));
        }
    }

    Runnable wakeUpRunnable = new Runnable() {
        @Override
        public void run() {
            VideoScannerThread.getInstance(mContext).wakeUp();
        }
    };
}
